package org.darkforest.admin.controller;

import org.apache.commons.lang.StringUtils;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.common.services.CmsSpecialCommentService;
import org.darkforest.common.services.StoreService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.CmsSpecialMapper;
import org.darkforest.dao.mapper.CountySupportPoorMapper;
import org.darkforest.dao.mapper.EnumParamMapper;
import org.darkforest.model.CmsSpecialComment;
import org.darkforest.model.CmsSpecialWithBLOBs;
import org.darkforest.model.CountySupportPoor;
import org.darkforest.model.EnumParam;
import org.darkforest.model.Store;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 扶贫风采
 *
 * @auth bingo
 * @create 2017-11-22-18:47
 */
@Controller
@RequestMapping(value = "operation")
public class SupportPoorController {

    @Autowired
    private CmsSpecialMapper cmsSpecialMapper;


    @Autowired
    private EnumParamMapper enumParamMapper;

    @Autowired
    private StoreService storeService;

    @Autowired
    private CmsSpecialCommentService specialCommentService;

    @Autowired
    private CountySupportPoorMapper countySupportPoorMapper;

    /**
     * 扶贫风采详情
     * @param start
     * @param specialTitle
     * @return
     */
    @RequestMapping(value = "supportPoorList")
    public ModelAndView supportPoorList(@RequestParam(defaultValue = "1" , required = false)int start ,
                                        String specialTitle){

        ModelAndView res = new ModelAndView("operation/supportpoor/support_poor_list");
        CmsSpecialWithBLOBs cmsSpecialWithBLOBs = new CmsSpecialWithBLOBs();
        cmsSpecialWithBLOBs.setStart(start);
        cmsSpecialWithBLOBs.setSpecialTitle(StringUtils.isBlank(specialTitle) ? null : specialTitle);
        List<CmsSpecialWithBLOBs> cmsSpecialWithBLOBsList = cmsSpecialMapper.selectByCondtion(cmsSpecialWithBLOBs);
        cmsSpecialWithBLOBs.setResultList(cmsSpecialWithBLOBsList);
        res.addObject("page",cmsSpecialWithBLOBs);
        return res;
    }


    /**
     * 跳转到新增扶贫风采页面
     * @return
     */
    @RequestMapping(value = "supportPoorList/toAddSupportPoor")
    public ModelAndView toAddSupportPoor(){

        ModelAndView res = new ModelAndView("operation/supportpoor/add_support_poor");
        Store store = new Store();
        store.setScId(1);
        store.setPageSwitch(false);
        List<Store> stores = storeService.findStoreByCondition(store);

//        EnumParam enumParam =new EnumParam();
//        enumParam.setPageSwitch(false);
//        enumParam.setParamType("county");
//        enumParam.setParamState(1);
//        List<EnumParam> countyList = enumParamMapper.selectListByCondition(enumParam);
        CountySupportPoor countySupportPoor = new CountySupportPoor();
        countySupportPoor.setPageSwitch(false);
        List<CountySupportPoor> countyList = countySupportPoorMapper.selectListByCondition(countySupportPoor);

        res.addObject("stores",stores);
        res.addObject("countyList",countyList);

        return res;
    }

    /**
     * 新增扶贫风采
     * @param cmsSpecialWithBLOBs
     * @param session
     * @return
     */
    @ArchivesLog(operationDescription = "新增贫风专题")
    @RequestMapping(value = "doAddSupportPoor")
    public @ResponseBody boolean doAddSupportPoor(CmsSpecialWithBLOBs cmsSpecialWithBLOBs,HttpSession session){
        MemberBase memberBase = (MemberBase)session.getAttribute("memInfo");
        cmsSpecialWithBLOBs.setSpecialPublishId(memberBase.getUserId());
        cmsSpecialWithBLOBs.setSpecialModifyTime(DateUtil.unixTimeStampInt().intValue());
        int i = cmsSpecialMapper.insertSelective(cmsSpecialWithBLOBs);
        return i == 1?true:false;

    }


    /**
     * 跳转到修改扶贫风采页面
     * @return
     */
    @RequestMapping(value = "supportPoorList/toEditSupportPoor")
    public ModelAndView toEditSupportPoor(int id){
        CmsSpecialWithBLOBs cmsSpecialWithBLOBs = cmsSpecialMapper.selectByPrimaryKey(id);
        ModelAndView res = new ModelAndView("operation/supportpoor/edit_support_poor");
        Store store = new Store();
        store.setScId(1);
        store.setPageSwitch(false);
        List<Store> stores = storeService.findStoreByCondition(store);

//        EnumParam enumParam =new EnumParam();
//        enumParam.setPageSwitch(false);
//        enumParam.setParamType("county");
//        enumParam.setParamState(1);
//        List<EnumParam> countyList = enumParamMapper.selectListByCondition(enumParam);
        CountySupportPoor countySupportPoor = new CountySupportPoor();
        countySupportPoor.setPageSwitch(false);
        List<CountySupportPoor> countyList = countySupportPoorMapper.selectListByCondition(countySupportPoor);

        res.addObject("countyList",countyList);
        res.addObject("stores",stores);
        res.addObject("bean",cmsSpecialWithBLOBs);
        return res;
    }

    /**
     * 修改扶贫风采
     * @param cmsSpecialWithBLOBs
     * @param session
     * @return
     */
    @ArchivesLog(operationDescription = "修改贫风专题")
    @RequestMapping(value = "doEditSupportPoor")
    public @ResponseBody boolean doEditSupportPoor(CmsSpecialWithBLOBs cmsSpecialWithBLOBs,HttpSession session){
        MemberBase memberBase = (MemberBase)session.getAttribute("memInfo");
        cmsSpecialWithBLOBs.setSpecialPublishId(memberBase.getUserId());
        cmsSpecialWithBLOBs.setSpecialModifyTime(DateUtil.unixTimeStampInt().intValue());
        int i = cmsSpecialMapper.updateByPrimaryKeySelective(cmsSpecialWithBLOBs);
        return i == 1?true:false;
    }


    @ArchivesLog(operationDescription = "置顶扶贫风专题")
    @RequestMapping(value = "setSupportPoorTop")
    public @ResponseBody boolean setSupportPoorTop(int id){
        //取消所有置顶
        cmsSpecialMapper.cancelAllTop();
        CmsSpecialWithBLOBs cmsSpecialWithBLOBs = new CmsSpecialWithBLOBs();
        cmsSpecialWithBLOBs.setSpecialId(id);
        cmsSpecialWithBLOBs.setSpecialFirst(true);
        int i = cmsSpecialMapper.updateByPrimaryKeySelective(cmsSpecialWithBLOBs);
        return i == 1?true:false;
    }


    @ArchivesLog(operationDescription = "设置扶贫专题状态")
    @RequestMapping(value = "setSpecialState")
    public @ResponseBody boolean setSpecialState(int id,int state){
        CmsSpecialWithBLOBs cmsSpecialWithBLOBs = new CmsSpecialWithBLOBs();
        cmsSpecialWithBLOBs.setSpecialId(id);
        cmsSpecialWithBLOBs.setSpecialState(state);
        int i = cmsSpecialMapper.updateByPrimaryKeySelective(cmsSpecialWithBLOBs);
        return i == 1?true:false;
    }

    @ArchivesLog(operationDescription = "删除扶贫专题状态")
    @RequestMapping(value = "delSpecialState")
    public @ResponseBody boolean delSpecialState(int id){
        int i = cmsSpecialMapper.deleteByPrimaryKey(id);
        return i == 1?true:false;
    }


    /**
     * 留言列表
     * @return
     */
    @RequestMapping("supportPoorList/specialComment")
    public ModelAndView specialComment(int id, @RequestParam(defaultValue = "1" , required = false)int start){
        ModelAndView modelAndView = new ModelAndView("operation/supportpoor/specialComment_edit");
        CmsSpecialComment page = new CmsSpecialComment();
        page.setSpecialId(id);
        page.setStart(start);
//        page.setPageSize(pageSize);
        page.setResultList(specialCommentService.selectListByCondition(page));
        modelAndView.addObject("page",page);
        return modelAndView;
    }

    @ArchivesLog(operationDescription = "设置留言显示状态")
    @RequestMapping(value = "supportPoorList/setSpecialCommentStatus")
    public @ResponseBody boolean setSpecialCommentStatus(int id,int state){
        CmsSpecialComment cmsSpecialComment = new CmsSpecialComment();
        cmsSpecialComment.setCommentId(id);
        cmsSpecialComment.setCommentStatus((byte)state);
        int i = specialCommentService.updateByPrimaryKeySelective(cmsSpecialComment);
        return i == 1?true:false;
    }
}
